package test;

import java.io.IOException;

import java.util.Scanner;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.thrift.generated.Hbase.AsyncProcessor.scannerClose;

public class Count {
	public static Configuration configuration;
	public static Connection connection;
	public static Admin admin;

	public static void init() {
		configuration = HBaseConfiguration.create();
		configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
		try {
			connection = ConnectionFactory.createConnection(configuration);
			admin = connection.getAdmin();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void close() {
		try {
			if (admin != null)
				admin.close();
			if (null != connection)
				connection.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void countRows(String name) throws IOException{
		init();
		Table table=connection.getTable(TableName.valueOf(name));
		Scan scan=new Scan();
		ResultScanner scanner=table.getScanner(scan);
		int num=0;
		for(Result result =scanner.next();result!=null;result=scanner.next())
			num++;
		System.out.println("行数："+num);
		scanner.close();
		close();
	}
	public static void main(String[] args) throws IOException{
		Count c=new Count();
		System.out.println("student统计行数");
		c.countRows("student");
	}
}
